//
//  HomePageViewController.m
//  WorkNest
//
//  Created by qbm360 on 15/7/7.
//  Copyright (c) 2015年 nest. All rights reserved.
//

#import "HomePageViewController.h"
#import "HomePageCollectionCell.h"
#import "PriceQueryViewController.h"
#import "OrderViewController.h"
#import "MyProductViewController.h"
#import "ProductDetailsViewController.h"
#import "PlatformStyleViewController.h"
#import "HomePageHeadCell.h"
#import "MyInfomationViewController.h"
#import "ProcessIntroductionViewController.h"

@interface HomePageViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *jsonArray;

@end

static NSString *const homeIdentifier = @"HomePageCollectionCell";

@implementation HomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setLeftBarButtonItem:nil];
    [self titleView:@"首页"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"HomePageCollectionCell" bundle:nil]
          forCellWithReuseIdentifier:homeIdentifier];
    self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
    [self.collectionView.header beginRefreshing];
    [self navigationRightItem];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateInfoCount) name:@"tuisong" object:nil];
}

- (void)updateInfoCount {
    [self navigationRightItem];
}

- (void)navigationRightItem {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 40, 30);
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"infomation"]];
    imageView.frame=CGRectMake(15, 7, 18, 17);
    [button addSubview:imageView];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(27, 1, 13, 13)];
    label.text = [NSString stringWithFormat:@"%ld",(long)[[UIApplication sharedApplication]applicationIconBadgeNumber]];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:10];
    label.backgroundColor = [UIUtil colorWithHexString:@"#F32E14"];
    label.layer.cornerRadius = 6.5;
    label.layer.borderWidth = 1;
    label.layer.masksToBounds = YES;
    label.layer.borderColor = [[UIUtil colorWithHexString:@"#F32E14"] CGColor];
    [button addSubview:label];
    [button addTarget:self action:@selector(gotoInfo) forControlEvents:UIControlEventTouchUpInside];
    label.hidden = [label.text isEqualToString:@"0"]?YES:NO;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)requestData {
    NSDictionary *dic = @{@"type":@"2", @"productType":@"",@"productStyle":@"",@"designerId":@""};
    [RequestData requestProductParameters:[UIUtil toJsonString:dic] withBlock:^(NSArray *jsonArray, NSString *msg, NSError *error) {
        if (error) {
            [self.collectionView.header endRefreshing];
            if ([msg isEqualToString:@"9001"] || [msg isEqualToString:@"9002"]) {
                [UIUtil tokenOverdue:self msg:msg];
            } else {
                [[ESHUDView sharedInstance] showErrorMessage:msg==nil?@"请求失败":msg dismissBlock:nil];
            }
        } else {
            [self.collectionView.header endRefreshing];
            self.jsonArray = jsonArray;
            [self.collectionView reloadData];
        }
    }];
}

#pragma mark - Action Methods
- (void)gotoInfo {
    MyInfomationViewController *view = [[MyInfomationViewController alloc] init];
    [view setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:view animated:YES];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.jsonArray.count + 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        static NSString *const homeIdentifier1 = @"HomePageHeadCell";
        [self.collectionView registerNib:[UINib nibWithNibName:@"HomePageHeadCell" bundle:nil]
              forCellWithReuseIdentifier:homeIdentifier1];
        HomePageHeadCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homeIdentifier1 forIndexPath:indexPath];
        cell.HomeBlock = ^(NSInteger index){
            switch (index) {
                case 0:{
                    PriceQueryViewController *view = [[PriceQueryViewController alloc] init];
                    view.priceTitle = @"询价单";
                    view.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:view animated:YES];
                    break;
                }
                case 1: {
                    PriceQueryViewController *view = [[PriceQueryViewController alloc] init];
                    view.priceTitle = @"制版打样";
                    view.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:view animated:YES];
                    break;
                }
                case 2: {
                    OrderViewController *view = [[OrderViewController alloc] init];
                    view.ifTab = NO;
                    view.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:view animated:YES];
                    break;
                }
                case 3: {
                    PlatformStyleViewController *view = [[PlatformStyleViewController alloc] init];
                    view.titleName = @"平台现款";
                    view.ifCategory = NO;
                    view.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:view animated:YES];
                    break;
                }
                case 4: {
                    MyProductViewController *view = [[MyProductViewController alloc] init];
                    view.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:view animated:YES];
                    break;
                }
                case 5: {
                    ProcessIntroductionViewController *view = [[ProcessIntroductionViewController alloc] init];
                    view.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:view animated:YES];
                    break;
                }
                default:
                    break;
            }
        };
        return cell;
    } else {
        HomePageCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homeIdentifier forIndexPath:indexPath];
        if (cell == nil) {
            cell = [[HomePageCollectionCell alloc] init];
        }
        cell.model = [_jsonArray objectAtIndex:indexPath.row - 1];
        return cell;
    }
}


#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row != 0) {
        ProductDetailsViewController *view = [[ProductDetailsViewController alloc] init];
        view.hidesBottomBarWhenPushed = YES;
        view.productId = ((ProductModel *)[_jsonArray objectAtIndex:indexPath.row - 1]).productId;
        view.productStatus = ((ProductModel *)[_jsonArray objectAtIndex:indexPath.row - 1]).productStatus;
        [self.navigationController pushViewController:view animated:YES];
    }
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return CGSizeMake(SCREEN_WIDTH, ((SCREEN_WIDTH-140)/3-61)*2+232);
//        return CGSizeMake(SCREEN_WIDTH, 232);
    } else {
        return CGSizeMake((SCREEN_WIDTH-6)/2, (SCREEN_WIDTH - 6)/2 * 188 / 157 +70);
    }
}

@end
